using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ListaFacil
{
    public partial class frmCadastroLogradouro : Form
    {
        private DataTable dtConsultaTipo;
        private DataTable dtConsultaBairro;
        private DataTable dtConsultaLogradouro;
        private BindingSource bsLogradouro;
        private bool editMode = false;

        public frmCadastroLogradouro()
        {
            InitializeComponent();
        }

        private void frmCadastroLogradouro_Load(object sender, EventArgs e)
        {
            this.bsLogradouro = new BindingSource();
            
            this.preencheComboTipo();

            this.preencheComboCidade();

            this.preencheGridLogradouro();

            this.HabilitaBotoes(true);
        }


        /// <summary>
        /// Preenche combo Bairro
        /// </summary>
        private void preencheGridLogradouro()
        {
            this.dtConsultaLogradouro = Program.SLF.ConsultaLogradouro();
            this.bsLogradouro.DataSource = this.dtConsultaLogradouro;
            this.bnLogradouro.BindingSource = this.bsLogradouro;

            this.dgvLogradouro.DataSource = this.bsLogradouro;


            //ESCONDE ID_CIDADE E ID_TIPO
            ((DataGridViewColumn)dgvLogradouro.Columns[2]).Visible = false;
            ((DataGridViewColumn)dgvLogradouro.Columns[3]).Visible = false;
            ((DataGridViewColumn)dgvLogradouro.Columns[5]).Visible = false;
            ((DataGridViewColumn)dgvLogradouro.Columns[6]).Visible = false;
            ((DataGridViewColumn)dgvLogradouro.Columns[7]).Visible = false;
        }

        /// <summary>
        /// Preenche combo Cidade
        /// </summary>
        private void preencheComboCidade()
        {
            this.dtConsultaBairro = Program.SLF.ConsultaCamposTabelas("Cidade");

            this.cboCidade.DataSource = this.dtConsultaBairro;
            this.cboCidade.DisplayMember = "Nome";
            this.cboCidade.ValueMember = "ID_Cidade";
        }

        /// <summary>
        /// Preenche combo Tipo
        /// </summary>
        private void preencheComboTipo()
        {
            //Consulta  * tabela Tipo
            this.dtConsultaTipo = Program.SLF.ConsultaCamposTabelas("Tipo");

            this.cboTipo.DataSource = this.dtConsultaTipo;
            this.cboTipo.DisplayMember = "Descricao";
            this.cboTipo.ValueMember = "ID_Tipo";
        }

        private void HabilitaBotoes(bool flagHabilita)
        {
            bindingNavigatorAddNewItem.Enabled = flagHabilita;
            bindingNavigatorDeleteItem.Enabled = flagHabilita;
            bindingNavigatorMoveFirstItem.Enabled = flagHabilita;
            bindingNavigatorMoveLastItem.Enabled = flagHabilita;
            bindingNavigatorMoveNextItem.Enabled = flagHabilita;
            bindingNavigatorMovePreviousItem.Enabled = flagHabilita;
            bindingNavigatorPositionItem.Enabled = flagHabilita;
            EdittoolStripButton.Enabled = flagHabilita;
            saveToolStripButton.Enabled = !flagHabilita;
            canceltoolStripButton.Enabled = !flagHabilita;
            txtLogradouro.Enabled = !flagHabilita;
            cboCidade.Enabled = !flagHabilita;
            cboTipo.Enabled = !flagHabilita;
            dgvLogradouro.Enabled = flagHabilita;
        }

        private void dgvLogradouro_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvLogradouro.CurrentRow != null)
            {
                cboTipo.SelectedValue = dgvLogradouro.CurrentRow.Cells[7].Value;
                txtLogradouro.Text = dgvLogradouro.CurrentRow.Cells[3].Value.ToString();
                cboCidade.SelectedValue = dgvLogradouro.CurrentRow.Cells[6].Value;
                this.Navegacao(this, e);
            }
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            this.HabilitaBotoes(false);
        }

        private void EdittoolStripButton_Click(object sender, EventArgs e)
        {
            this.HabilitaBotoes(false);
            this.editMode = true;
        }

        private void canceltoolStripButton_Click(object sender, EventArgs e)
        {
            this.preencheGridLogradouro();
            this.HabilitaBotoes(true);
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            int numeroregistro = 0;
            int linha = 0;
            string retorno = "";

            linha = Convert.ToInt32(bindingNavigatorPositionItem.Text)-1;
            numeroregistro = Convert.ToInt32(dgvLogradouro.Rows[linha].Cells[0].Value);

            retorno = Program.SLF.ApagaLogradouro(numeroregistro);
    
            if (retorno != "")
                MessageBox.Show(retorno, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
                MessageBox.Show("Sucesso ao excluir a rua de ID=" +numeroregistro , "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.preencheGridLogradouro();
            this.HabilitaBotoes(true);
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            string retorno = "";
            int numeroregistro = 0;
            int linha = 0;

            if (this.editMode == false)
            {
                retorno = Program.SLF.InsereLogradouro(this.txtLogradouro.Text.Trim(),
                                                       Convert.ToInt32(this.cboTipo.SelectedValue),
                                                       Convert.ToInt32(this.cboCidade.SelectedValue));

                if (retorno != "")
                    MessageBox.Show(retorno, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                linha = Convert.ToInt32(bindingNavigatorPositionItem.Text) -1;
                numeroregistro = Convert.ToInt32(dgvLogradouro.Rows[linha].Cells[0].Value);
                retorno = Program.SLF.UpdateLogradouro(this.txtLogradouro.Text.Trim(),
                                                       Convert.ToInt32(this.cboTipo.SelectedValue),
                                                       Convert.ToInt32(this.cboCidade.SelectedValue),
                                                       numeroregistro);

                if (retorno != "")
                    MessageBox.Show(retorno, "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            this.preencheGridLogradouro();
            this.HabilitaBotoes(true);
        }

        private void txtLogradouro_TextChanged(object sender, EventArgs e)
        {
            if (this.editMode)
                this.HabilitaBotoes(false);
        }

        private void bindingNavigatorPositionItem_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsDigit(e.KeyChar) == false)
                e.Handled = true;
        }

        private void Navegacao(object sender, EventArgs e)
        {
            this.editMode = false;
        }

        private void Edicao(object sender, EventArgs e)
        {
            this.editMode = true;
        }
    }
}